package com.example.nbshopping.utils;
import  com.example.nbshopping.R;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;;
import android.content.Intent;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.nbshopping.com.example.nbshopping.login_reg.LoginRegActivity;
import com.example.nbshopping.com.example.nbshopping.login_reg.PersonCenterActivity;
import com.example.nbshopping.com.example.nbshopping.login_reg.UserBean;
import com.example.nbshopping.password.PwdQuestionActivity;


public class SettingActivity extends AppCompatActivity{
    Button person_center, pwdchange, exit;
    ImageView back;
    UserBean.DataBean userInfo;
    String question;
    int id;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        initView();
        setListener();
    }

    private void initView() {
        person_center = findViewById(R.id.set_btn_person);
        pwdchange =findViewById(R.id.set_btn_change);
        exit = findViewById(R.id.set_btn_exit);
        back= findViewById(R.id.set_iv_back);
    }

    private void setListener() {
        person_center.setOnClickListener(onClickListener);
        pwdchange.setOnClickListener(onClickListener);
        exit.setOnClickListener(onClickListener);
        back.setOnClickListener(onClickListener);
    }
    private void exitlogin() {
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(SettingActivity.this);
        if (userInfo == null) {
            Toast.makeText(this,"您还未登陆账号！",Toast.LENGTH_SHORT).show();
        }else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示信息").setMessage("您是否确定退出登录？")
                    .setPositiveButton("取消",null)
                    .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //删除文件当中的内容
                            SaveUserUtils.deleteUserInfo(SettingActivity.this);
                            Toast.makeText(SettingActivity.this,"注销成功！",Toast.LENGTH_SHORT).show();
                        }
                    });
            builder.create().show();
        }
    }
    private void islogin(int flag) {
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(this);
        Log.i("mjw123", "islogin: user=="+userInfo);
        if (userInfo == null) {
            /*判断是否登录，如果登录就跳转个人中心页面，如果没有登陆，就跳转登陆页面*/
            Toast.makeText(this,"您还未登陆账号！",Toast.LENGTH_SHORT).show();
            Intent it1 = new Intent(SettingActivity.this, LoginRegActivity.class);
            //跳转页面
            it1.putExtra("flag", flag);
            startActivity(it1);
        } else {
            Intent intent = new Intent(SettingActivity.this, PersonCenterActivity.class);
            startActivity(intent);
        }
    }
    private void judgelogin(int flag) {
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(this);
        Log.i("mjw123", "islogin: user=="+userInfo);
        if (userInfo == null) {
            /*判断是否登录，如果登录就跳转个人中心页面，如果没有登陆，就跳转登陆页面*/
            Toast.makeText(this,"您还未登陆账号！",Toast.LENGTH_SHORT).show();
        } else {
            userInfo = SaveUserUtils.getUserInfo(this);
            question= userInfo.getQuestion();
            id= userInfo.getId();
            Intent intent = new Intent(SettingActivity.this, PwdQuestionActivity.class);
            intent.putExtra("question",question);
            intent.putExtra("id",id);
            startActivity(intent);
        }
    }
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id = v.getId();  // 获取到被点击的id
            switch (id) {
                case R.id.set_iv_back:
                    finish();
                    break;
                case R.id.set_btn_person:
                    islogin(0);
                    break;
                case R.id.set_btn_change:

                    judgelogin(0);
                    break;
                case R.id.set_btn_exit:
                    exitlogin();
                    break;
            }
        }
    };
}